package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmThreadService;
import com.mdd.admin.validate.ClientCrmThreadCreateValidate;
import com.mdd.admin.validate.ClientCrmThreadUpdateValidate;
import com.mdd.admin.validate.ClientCrmThreadSearchValidate;
import com.mdd.admin.vo.ClientCrmThreadListedVo;
import com.mdd.admin.vo.ClientCrmThreadDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmThread;
import com.mdd.common.mapper.ClientCrmThreadMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 线索zsgc实现类
 * @author 洁洁
 */
@Service
public class ClientCrmThreadServiceImpl implements IClientCrmThreadService {
        
    @Resource
    ClientCrmThreadMapper clientCrmThreadMapper;

    /**
     * 线索zsgc列表
     *
     * @author 洁洁
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmThreadListedVo>
     */
    @Override
    public PageResult<ClientCrmThreadListedVo> list(PageValidate pageValidate, ClientCrmThreadSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmThread> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmThreadMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "like:deptName@dept_name:str",
            "like:name:str",
            "=:phone:str",
            "=:number:str",
            "=:leadSource@lead_source:int",
            "between:firstMeetingsTime@first_meetings_time:str",
            "=:estimatedSalesAmount@estimated_sales_amount:str",
            "between:expectedOrderTime@expected_order_time:str",
            "=:intendedBusiness@Intended_business:str",
            "=:businessPersonnelId@business_personnel_id:int",
            "=:collaboratorId@collaborator_id:int",
            "=:type:str",
            "between:newFollowUpTime@new_follow_up_time:str",
            "=:userId@user_id:int",
            "datetime:createTimeStart-createTimeEnd@create_time:str",
        });

        IPage<ClientCrmThread> iPage = clientCrmThreadMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmThreadListedVo> list = new LinkedList<>();
        for(ClientCrmThread item : iPage.getRecords()) {
            ClientCrmThreadListedVo vo = new ClientCrmThreadListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 线索zsgc详情
     *
     * @author 洁洁
     * @param id 主键参数
     * @return ClientCrmThread
     */
    @Override
    public ClientCrmThreadDetailVo detail(Integer id) {
        ClientCrmThread model = clientCrmThreadMapper.selectOne(
                new QueryWrapper<ClientCrmThread>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmThreadDetailVo vo = new ClientCrmThreadDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setCreateTime(vo.getCreateTime()!=null?TimeUtils.timestampToDate(vo.getCreateTime()):null);
        vo.setUpdateTime(vo.getUpdateTime()!=null?TimeUtils.timestampToDate(vo.getUpdateTime()):null);
        return vo;
    }

    /**
     * 线索zsgc新增
     *
     * @author 洁洁
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmThreadCreateValidate createValidate) {
        ClientCrmThread model = new ClientCrmThread();
        model.setCustomerName(createValidate.getCustomerName());
        model.setDeptName(createValidate.getDeptName());
        model.setHttpUrl(createValidate.getHttpUrl());
        model.setName(createValidate.getName());
        model.setPost(createValidate.getPost());
        model.setPhone(createValidate.getPhone());
        model.setNumber(createValidate.getNumber());
        model.setWechatNo(createValidate.getWechatNo());
        model.setQq(createValidate.getQq());
        model.setEmail(createValidate.getEmail());
        model.setAddress(createValidate.getAddress());
        model.setPostcode(createValidate.getPostcode());
        model.setKeyIndustry(createValidate.getKeyIndustry());
        model.setIntellectualPropertyAddress(createValidate.getIntellectualPropertyAddress());
        model.setIntellectualPropertyNumber(createValidate.getIntellectualPropertyNumber());
        model.setInternalCaseSource(createValidate.getInternalCaseSource());
        model.setInternalCaseSourcePhone(createValidate.getInternalCaseSourcePhone());
        model.setInternalCaseSourceEmail(createValidate.getInternalCaseSourceEmail());
        model.setExternalCaseSource(createValidate.getExternalCaseSource());
        model.setExternalCaseSourcePhone(createValidate.getExternalCaseSourcePhone());
        model.setExternalCaseSourceEmail(createValidate.getExternalCaseSourceEmail());
        model.setCaseSourceType(createValidate.getCaseSourceType());
        model.setCountry(createValidate.getCountry());
        model.setProvince(createValidate.getProvince());
        model.setCity(createValidate.getCity());
        model.setAdministrativeDivision(createValidate.getAdministrativeDivision());
        model.setMainCompanyId(createValidate.getMainCompanyId());
        model.setSubsidiaryCompanyId(createValidate.getSubsidiaryCompanyId());
        model.setLeadSource(createValidate.getLeadSource());
        model.setRemark(createValidate.getRemark());
        model.setFirstMeetingsTime(createValidate.getFirstMeetingsTime());
        model.setFollowUpStatus(createValidate.getFollowUpStatus());
        model.setFollowUpNextTime(createValidate.getFollowUpNextTime());
        model.setEstimatedSalesAmount(createValidate.getEstimatedSalesAmount());
        model.setExpectedOrderTime(createValidate.getExpectedOrderTime());
        model.setIntendedBusiness(createValidate.getIntendedBusiness());
        model.setBusinessPersonnelId(createValidate.getBusinessPersonnelId());
        model.setCollaboratorId(createValidate.getCollaboratorId());
        model.setFormerBusinessPersonnelId(createValidate.getFormerBusinessPersonnelId());
        model.setType(createValidate.getType());
        model.setUserId(createValidate.getUserId());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmThreadMapper.insert(model);
    }

    /**
     * 线索zsgc编辑
     *
     * @author 洁洁
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmThreadUpdateValidate updateValidate) {
        ClientCrmThread model = clientCrmThreadMapper.selectOne(
                new QueryWrapper<ClientCrmThread>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setDeptName(updateValidate.getDeptName());
        model.setHttpUrl(updateValidate.getHttpUrl());
        model.setName(updateValidate.getName());
        model.setPost(updateValidate.getPost());
        model.setPhone(updateValidate.getPhone());
        model.setNumber(updateValidate.getNumber());
        model.setWechatNo(updateValidate.getWechatNo());
        model.setQq(updateValidate.getQq());
        model.setEmail(updateValidate.getEmail());
        model.setAddress(updateValidate.getAddress());
        model.setPostcode(updateValidate.getPostcode());
        model.setKeyIndustry(updateValidate.getKeyIndustry());
        model.setIntellectualPropertyAddress(updateValidate.getIntellectualPropertyAddress());
        model.setIntellectualPropertyNumber(updateValidate.getIntellectualPropertyNumber());
        model.setInternalCaseSource(updateValidate.getInternalCaseSource());
        model.setInternalCaseSourcePhone(updateValidate.getInternalCaseSourcePhone());
        model.setInternalCaseSourceEmail(updateValidate.getInternalCaseSourceEmail());
        model.setExternalCaseSource(updateValidate.getExternalCaseSource());
        model.setExternalCaseSourcePhone(updateValidate.getExternalCaseSourcePhone());
        model.setExternalCaseSourceEmail(updateValidate.getExternalCaseSourceEmail());
        model.setCaseSourceType(updateValidate.getCaseSourceType());
        model.setCountry(updateValidate.getCountry());
        model.setProvince(updateValidate.getProvince());
        model.setCity(updateValidate.getCity());
        model.setAdministrativeDivision(updateValidate.getAdministrativeDivision());
        model.setMainCompanyId(updateValidate.getMainCompanyId());
        model.setSubsidiaryCompanyId(updateValidate.getSubsidiaryCompanyId());
        model.setLeadSource(updateValidate.getLeadSource());
        model.setRemark(updateValidate.getRemark());
        model.setFirstMeetingsTime(updateValidate.getFirstMeetingsTime());
        model.setFollowUpStatus(updateValidate.getFollowUpStatus());
        model.setFollowUpNextTime(updateValidate.getFollowUpNextTime());
        model.setEstimatedSalesAmount(updateValidate.getEstimatedSalesAmount());
        model.setExpectedOrderTime(updateValidate.getExpectedOrderTime());
        model.setIntendedBusiness(updateValidate.getIntendedBusiness());
        model.setBusinessPersonnelId(updateValidate.getBusinessPersonnelId());
        model.setCollaboratorId(updateValidate.getCollaboratorId());
        model.setFormerBusinessPersonnelId(updateValidate.getFormerBusinessPersonnelId());
        model.setType(updateValidate.getType());
        model.setUserId(updateValidate.getUserId());
        clientCrmThreadMapper.updateById(model);
    }

    /**
     * 线索zsgc删除
     *
     * @author 洁洁
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmThread model = clientCrmThreadMapper.selectOne(
                new QueryWrapper<ClientCrmThread>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmThreadMapper.updateById(model);
    }

}
